﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;

namespace HRM.Web.master
{
    public partial class Main : System.Web.UI.MasterPage
    {
        #region Global
        String BasePath;
        #endregion

        #region PageLoad and Method

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BasePath = "http://" + HttpContext.Current.Request.Url.Authority;
                GenerateMenu();
                SetBreadCrumb();
            }
        }

        private void SetBreadCrumb()
        {
            if (Request.QueryString["mid"] != null && Request.QueryString["mid"].ToString() != "")
            {
                BLL.Menu objMenu = new BLL.Menu();
                string breadCrumb = objMenu.GetBreadCrumb(Convert.ToInt32(Request.QueryString["mid"].ToString()));
                divBreadCrumb.InnerHtml = breadCrumb;
                Session["PageName"] = breadCrumb.Split('-').Last().Trim();
            }
            else
            {
                Session["PageName"] = "";
            }
        }
        #endregion

        #region Modules and Left menu

        void GenerateMenu()
        {
            if (Request.QueryString["vid"] != null)
            {
                Session["vid"] = Request.QueryString["vid"].ToString();
            }
            if (Request.QueryString["mid"] != null)
            {
                Session["mid"] = Request.QueryString["mid"].ToString();
            }


            BLL.Menu objMenu = new BLL.Menu();
            DataTable dt = objMenu.GetAllMenus();

            string[] TobeDistinct = { "ModuleID", "ModuleName" };

            string strModuleID = "";

            #region Get and Bind Parent Modules

            DataTable dtTemp = dt;
            DataTable drModules = GetDistinctRecords(dtTemp, TobeDistinct);

            if (drModules.Rows.Count > 0)
            {
                strModuleID = " AND ModuleID=" + drModules.Rows[0]["ModuleID"].ToString();
                StringBuilder sbModules = new StringBuilder();

                foreach (DataRow dr in drModules.Rows)
                {
                    sbModules.Append("<div class='item'><a href='" + BasePath + "/forms/dashboard.aspx?ModuleID=" + dr["ModuleID"].ToString() + "' id=" + dr["ModuleID"].ToString() + " class='btn btn-primary btn-small'>" + dr["ModuleName"].ToString() + "</a></div>");
                }

                litModules.Text = sbModules.ToString();
            }
            #endregion

            #region Get and Bind Left Menu by ModuleID
            strModuleID = Request.QueryString["ModuleID"] != null && Request.QueryString["ModuleID"].ToString() != "" ? " AND ModuleID=" + Request.QueryString["ModuleID"].ToString() : strModuleID;

            DataRow[] drParents = dt.Select("ParentID is null" + strModuleID);
            StringBuilder sbMenu = new StringBuilder();
            foreach (DataRow dr in drParents)
            {
                if (dr["ID"].ToString() != "")
                {
                    sbMenu.Append("<ul id=" + dr["ModuleID"].ToString() + " class='navigation'>");
                    Int32 ParentID = Convert.ToInt32(dr["ID"].ToString());
                    String Caption = dr["Caption"].ToString();
                    String PageToRedirect = BasePath + "/" + dr["PageToRedirect"].ToString() + "?vid=" + dr["ModuleID"].ToString();
                    DataRow[] drSubMenu = dt.Select("ParentID = " + ParentID.ToString());
                    if (drSubMenu.Count() > 0)
                    {
                        sbMenu.Append(GenerateSubMenu(Caption, PageToRedirect, drSubMenu, dt));
                    }
                    else
                    {
                        if (Session["mid"] != null)
                        {
                            if (Session["mid"].ToString() == dr["ID"].ToString())
                            {
                                sbMenu.Append("<li class='active'><a href='" + PageToRedirect + "&mid=" + dr["ID"].ToString() + "'>" + Caption + "</a> </li>");
                            }
                            else
                            {
                                sbMenu.Append("<li><a href='" + PageToRedirect + "&mid=" + dr["ID"].ToString() + "'>" + Caption + "</a> </li>");
                            }
                        }
                        else
                        {
                            sbMenu.Append("<li><a href='" + PageToRedirect + "&mid=" + dr["ID"].ToString() + "'>" + Caption + "</a> </li>");
                        }
                        
                    }
                    sbMenu.Append("</ul>");
                }
            }
            ltrNav.Text = sbMenu.ToString();

            #endregion

        }

        String GenerateSubMenu(String ParentCaption, String redirectLink, DataRow[] drMenu, DataTable dt)
        {
            StringBuilder sbMenu = new StringBuilder();
            string cssClass = "openable";
            if (Session["mid"] != null)
            {
                DataRow[] drParentsID = dt.Select("ID=" + Session["mid"].ToString());
                if (drParentsID.Count() > 0)
                {

                    if (drParentsID[0]["ParentID"].ToString() == drMenu[0]["ParentID"].ToString())
                    {

                        cssClass = "openable active open";
                    }
                    else
                    {
                        cssClass = "openable";
                    }
                }
            }
            sbMenu.Append("<li class='" + cssClass + "'><a href='" + redirectLink + "'>" + ParentCaption + "</a>");
            sbMenu.Append("<ul>");
            foreach (DataRow dr in drMenu)
            {
                Int32 ParentID = Convert.ToInt32(dr["ID"].ToString());
                String Caption = dr["Caption"].ToString();
                String PageToRedirect = BasePath + "/" + dr["PageToRedirect"].ToString() + "?vid=" + dr["ModuleID"].ToString() + "&mid=" + dr["ID"].ToString();
                DataRow[] drSubMenu = dt.Select("ParentID = " + ParentID.ToString());

                if (drSubMenu.Count() > 0)
                {
                    sbMenu.Append(GenerateSubMenu(Caption, PageToRedirect, drSubMenu, dt));
                }
                else
                {
                    if (Session["mid"] != null)
                    {
                        if (Session["mid"].ToString() == dr["ID"].ToString())
                        {

                            sbMenu.Append("<li class='active'><a href='" + PageToRedirect + "'>" + Caption + "</a> </li>");
                        }
                        else
                        {
                            sbMenu.Append("<li><a href='" + PageToRedirect + "'>" + Caption + "</a> </li>");
                        }
                    }
                    else
                    {
                        sbMenu.Append("<li><a href='" + PageToRedirect + "'>" + Caption + "</a> </li>");
                    }
                }
            }
            sbMenu.Append("</ul></li>");
            return sbMenu.ToString();
        }

        public static DataTable GetDistinctRecords(DataTable dt, string[] Columns)
        {
            DataTable dtUniqRecords = new DataTable();
            dtUniqRecords = dt.DefaultView.ToTable(true, Columns);
            return dtUniqRecords;
        }
        #endregion

        #region Button click
        protected void lnkLogout_Click(object sender, EventArgs e)
        {
            Session.Abandon();
            Session.Clear();
            Response.Redirect("~/account/login.aspx");
        }
        #endregion

    }
}